/**
 * @Title: PreFilter.java
 * @Package com.xingyun.s2b2c.gateway.test
 * @Description: TODO(用一句话描述该文件做什么)
 * @author Administrator
 * @date 2018年9月11日 下午3:37:37
 * @version V1.0
 */
package com.gateway.filter;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.gateway.client.AuthApi;
import com.gateway.client.LoginInfo;
import com.common.global.R;
import com.netflix.loadbalancer.DynamicServerListLoadBalancer;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 前置处理
 */
@Component
@Slf4j
public class PreFilter extends ZuulFilter {

    @Autowired
    private AuthApi authApi;

    @Value("${token.filter.ignore:}")
    private String ignoreUrls;


    @SneakyThrows
    @Override
    public boolean shouldFilter() {
        AntPathMatcher matcher = new AntPathMatcher();
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        for (String ignoreUrl : Arrays.asList(ignoreUrls.split(";"))) {
            if (matcher.match(ignoreUrl, request.getServletPath())) {
                return false;
            }
        }
        return true;
    }

    @SneakyThrows
    @Override
    public Object run() {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        log.info("请求地址 :" + request.getRequestURI());

        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            throw new FilterException("非法访问");
        }

        R<LoginInfo> loginInfo = authApi.authenticationInfo(token);
        if (null == loginInfo || !String.valueOf(HttpStatus.OK.value()).equals(loginInfo.getCode())) {
            log.info(JSON.toJSONString(loginInfo));
            throw new FilterException(loginInfo.getMessage());
        }
        if (null == loginInfo.getData() || StringUtils.isEmpty(loginInfo.getData().getUserId())) {
            throw new FilterException("用户已过期");
        }
        context.addZuulRequestHeader("h-userName", loginInfo.getData().getUserName());
        context.addZuulRequestHeader("h-userId", loginInfo.getData().getUserId());
        context.addZuulRequestHeader("h-roles", JSON.toJSONString(loginInfo.getData().getRoles()));
        context.addZuulRequestHeader("h-auths", JSON.toJSONString(loginInfo.getData().getAuths()));
        return null;
    }

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER - 1;
    }

}
